﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using PloobsEngine.Audio;
using PloobsEngine.Draw;
using PloobsEngine.Entity;

namespace PloobsEngine.SceneControl
{
    /// <summary>
    /// Descreve uma tela do Jogo
    /// </summary>
    /// 
    public class SceneScreen : IScreen
    {
        #region properties

        private bool _isDebugDrawn = false;
        private IRenderTechnic[] _renderTecnics = null;
        private IRenderHelper _render;
        private bool _isFirstTimeTechnic = true;
        
        public IRenderHelper Render
        {
            set
            {
                this._render = value;
            }
            get
            {
                return this._render;
            }
        }

        public IRenderTechnic[] RenderTechnics
        {
            get
            {
                return _renderTecnics;
            }
            set
            {
                _isFirstTimeTechnic = true;
                this._renderTecnics = value;

            }
        }

        public bool isDebugDrawn
        {
            set
            {
                this._isDebugDrawn = value;
            }
            get
            {
                return this._isDebugDrawn;
            }
        }

        
        private IWorld _world = null;

        public IWorld World
        {
            set
            {
                this._world = value;
            }
            get
            {
                return this._world;
            }
        }

        public override void AfterLoadContent()
        {
            Drawing.setWorld(World);            

            foreach (IRenderTechnic item in RenderTechnics)
            {
                item.AfterLoadContent();

            }
        }
       

        #endregion

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {            
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            World.UpdateWorld(gameTime);
        }        

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (_isFirstTimeTechnic == true)
            {                
                foreach (IRenderTechnic item in RenderTechnics)
                {
                    item.Initialization(Render,this.World,es);
                }
                _isFirstTimeTechnic = false;
            }

            foreach (IRenderTechnic item in RenderTechnics)
            {                
                item.ExecuteTechnic(Render, this.World);
            }                 
            
        
            base.Draw(gameTime);
            
        }        

        public override void LoadContent(EngineStuff es)
        {
            Drawing.setEngine(es);
            if (_renderTecnics == null)
            {
                _renderTecnics = new IRenderTechnic[1];
                this._renderTecnics[0] = new DeferredRenderTechnic(es);
            }            
            this.es = es;            
            this._render = new RenderHelper(es);            
            base.LoadContent(es);
        }

        public override void UnloadContent()
        {
            base.UnloadContent();
        }

        
      
    }
}
